Table of Contents

Summary

This is a markdown template to produce HTML files. The template is a part of the biometrics package developed by PaulĀ Stevenson at Telethon Kids Institute. The template is built on bootstrap and the associated formatting tags should be available.

Introduction

Markdown Cheat Sheet

The markdown RStudio cheat sheat is here. Various information can be found here, including shortcuts to modify the text. Bootstrap typography codes also seem to be available to format markdown HTML documents; this feature has not been thoroughly tested by the author.

Referencing

Referencing important previous work and the scientific principles that underpin research is a vital aspect of any report. The default bibliography list that is used by this template is found in assets/bib/bibliography.bib. The author has included these references in the document for illustrative purposes (2017); however, they have nothing to do with anything that is written here (R Core Team 2018; Spinu, Grolemund, and Wickham 2018).

Bibliographic information for R packages are automatically generated (and loaded) with LoadandCite (Gandrud 2016). By default, the package bibliography is stored in assets/bib/packages.bib, which is overwritten on each Knit and should not be manually altered.

References are automatically placed at the end of the document, so it is important that a header, such as #Ā References, is placed at the end of your markdown document.

Adding R Script to Markdown

R chunks are added through the following blocks of code:

```{r chunk_name, include = FALSE}
Ā Ā print(ā€œHello World!ā€)
```

The above code produces the following in the markdown document:

## [1] "Hello World!"

The echo = FALSE argument can be used if you do not want the source code to be displayed:

## [1] "In this example, the source code has been hidden and only the output is displayed."

Output can be suppressed entirely by changing include = FALSE, or by removing the argument.

Methods

This template is built with the ProjectTemplate package in mind. By default, all data in the ā€œcacheā€ directories will be loaded. Files in the data directory are note loaded by default, but can be by setting data_loading = TRUE in the initialisation markdown chunk. Similarly, by default munge = FALSE and scripts in the munge directory will not be executed.

R’s included Iris data set will be used to illustrate how to include graphics and to use the colour palettes defined at the start of the document:

Results

Example Tables

Basic data

Kable is a very simple table generator that takes input as either a matrix or data frame (Zhu 2018); see the Kable documentation for styling options available.

In this template the kable formatting is applied automatically when using the chunk option chunk option ā€œtable = Tā€. By default, this template uses the hover style with full_width = FALSE. (Technical info: automatic formatting is implemented by kintr::knit_hooks() (Xie 2018) via the chunk source and optimal results are achieved when including the option results = ā€œhideā€. For the same reason, eval = F is included so that the chunk is not evaluated twice. The source code cannot be shown when the kable formatting is automatically applied.)

For example:

```{r, table = T, results = ā€œhideā€, eval = F}
Ā Ā head(iris)
```

will output:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

If custom table options are needed, then the kable formatting can be applied as normal:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

Model output

The broom::tidy (Robinson and Hayes 2018) function may be used to convert the output into a data frame that is compatible with kable When data is supplied from a model, which may be more appropraite for a report or publication.

For example, the summary output of a linear mixed effects model (lmr4::lmer) looks like:

Linear mixed model fit by REML ['lmerMod']
Formula: Sepal.Length ~ Sepal.Width + Petal.Width + (1 | Species)
   Data: iris

REML criterion at convergence: 190.6

Scaled residuals: 
     Min       1Q   Median       3Q      Max 
-2.78885 -0.55571 -0.05715  0.44177  2.85990 

Random effects:
 Groups   Name        Variance Std.Dev.
 Species  (Intercept) 0.3059   0.5531  
 Residual             0.1889   0.4346  
Number of obs: 150, groups:  Species, 3

Fixed effects:
            Estimate Std. Error t value
(Intercept)   3.2551     0.4597   7.082
Sepal.Width   0.6504     0.1152   5.645
Petal.Width   0.5000     0.1783   2.804

Correlation of Fixed Effects:
            (Intr) Spl.Wd
Sepal.Width -0.576       
Petal.Width -0.151 -0.410

Broom defines tidying methods for extracting three kinds of statistics from an object (see here):

  • tidy(): component-level statistics
  • augment(): observation-level statistics
  • glance(): model-level statistics

tidy():

term estimate std.error statistic group
(Intercept) 3.2551272 0.4596525 7.081714 fixed
Sepal.Width 0.6503987 0.1152174 5.644967 fixed
Petal.Width 0.5000449 0.1783392 2.803897 fixed
sd_(Intercept).Species 0.5530538 NA NA Species
sd_Observation.Residual 0.4345858 NA NA Residual

glance():

sigma logLik AIC BIC deviance df.residual
0.4345858 -95.30176 200.6035 215.6567 185.8619 145

augment():

Sepal.Length Sepal.Width Petal.Width Species .fitted .resid .cooksd .fixed .mu .offset .sqrtXwt .sqrtrwt .weights .wtres
5.1 3.5 0.2 setosa 5.037167 0.0628333 0.0001542 5.631532 5.037167 0 1 1 1 0.0628333
4.9 3.0 0.2 setosa 4.711967 0.1880326 0.0020478 5.306332 4.711967 0 1 1 1 0.1880326
4.7 3.2 0.2 setosa 4.842047 -0.1420471 0.0008486 5.436412 4.842047 0 1 1 1 -0.1420471
4.6 3.1 0.2 setosa 4.777007 -0.1770072 0.0015210 5.371372 4.777007 0 1 1 1 -0.1770072
5.0 3.6 0.2 setosa 5.102207 -0.1022066 0.0004543 5.696572 5.102207 0 1 1 1 -0.1022066
5.4 3.9 0.4 setosa 5.397335 0.0026648 0.0000004 5.991700 5.397335 0 1 1 1 0.0026648

Example Plots

The GGally::ggpairs function may be used to produce a visual summary of the input data:

Colour schemes

There are three colour schemes built into this theme: scale_colour_telethonkids(ā€œlightā€), scale_colour_telethonkids(ā€œdarkā€) and scale_colour_telethonkids(ā€œgreyā€). These colour themes are implemented by adding the appropriate theme to the ggplot code:

A continuous colour theme can be implemented by adding the argument discrete = FALSE the scale_colour_telethonkids():

Bar plots can also be ā€˜filled’ by discrete variables with differnt palettes by using scale_fill_telethonkids(palette = ā€œmixedā€, guide = ā€œnoneā€).

To ensure the image is responsive on different sized screens, the code out.extra = ā€œfigureā€ should be included in the R chunk options (this calls an option hook defined in hooks.R).

References

Gandrud, Christopher. 2016. Repmis: Miscellaneous Tools for Reproducible Research. https://CRAN.R-project.org/package=repmis.

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Robinson, David, and Alex Hayes. 2018. Broom: Convert Statistical Analysis Objects into Tidy Tibbles. https://CRAN.R-project.org/package=broom.

Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2018. Lubridate: Make Dealing with Dates a Little Easier. https://CRAN.R-project.org/package=lubridate.

Stevenson, Paul G., Niki K. Burns, Stuart D. Purcell, Paul S. Francis, Neil W. Barnett, Fiona Fry, and Xavier A. Conlan. 2017. ā€œApplication of 2D-Hplc Coupled with Principal Component Analysis to Study an Industrial Opiate Processing Stream.ā€ Talanta 166: 119–25. https://doi.org/10.1016/j.talanta.2017.01.044.

Xie, Yihui. 2018. Knitr: A General-Purpose Package for Dynamic Report Generation in R. https://CRAN.R-project.org/package=knitr.

Zhu, Hao. 2018. KableExtra: Construct Complex Table with ’Kable’ and Pipe Syntax. https://CRAN.R-project.org/package=kableExtra.